package com.demo.lab.controller;


import cn.hutool.core.util.RandomUtil;
import com.demo.lab.entity.LabRequisition;
import com.demo.lab.entity.Menu;
import com.demo.lab.entity.Summation;
import com.demo.lab.entity.User;
import com.demo.lab.entity.base.Result;
import com.demo.lab.service.UserService;
import com.demo.lab.utils.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/index/")
public class IndexController extends BaseController {

    @Autowired
    UserService userService;

    /**
     * Describe: 获取首页
     * Param ModelAndView
     * Return 首页视图
     */
    @GetMapping("myindex")
    public ModelAndView index(HttpServletRequest request, Model model) {
        //将用户放入model中
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        User user = userService.findOneById(userId);
        model.addAttribute("user", user);
        return jumpPage("index");
    }

    /**
     * Describe: 获取菜单列表
     * Param null
     * Return 菜单列表
     */
    @RequestMapping("menu")
    @ResponseBody
    public List<Menu> getMenu() {
        //根据权限从数据库获取菜单
        List<Menu> menu = new ArrayList<>();
        User user = (User) ServletUtil.getSession().getAttribute("user");
        if (user.getRoleId() == 0) {
            //管理员
            menu = userService.getMenuListToAdmin();
        } else if (user.getRoleId() == 1) {
            //科研人员用户菜单
            menu = userService.getMenuListToTeacher();
        } else if (user.getRoleId() == 2) {
            //学生用户
            menu = userService.getMenuListToUser();
        }
        return menu;
    }

    /*
     * @Description: 获取首页内容页面
     * @Param:
     * @return:
     */
    @GetMapping("console")
    public ModelAndView console(Model model) {
        User user = (User) ServletUtil.getSession().getAttribute("user");
        if (user.getRoleId() == 0) {
            //管理员登陆
            int unOutDevice = RandomUtil.randomInt(3);
            int unIntoDevice = RandomUtil.randomInt(3);
            int unLab = RandomUtil.randomInt(3);
            int unLabWork = RandomUtil.randomInt(3);
            model.addAttribute("unOutDevice", unOutDevice);
            model.addAttribute("unIntoDevice", unIntoDevice);
            model.addAttribute("unLab", unLab);
            model.addAttribute("unLabWork", unLabWork);
            //获取待审批的实验室领用单
            model.addAttribute("labList", new ArrayList<>());
            //获取最近登陆的五个用户
            List<User> userList = userService.lastLoginUser();
            model.addAttribute("userList", userList);
            return jumpPage("console");
        } else {
            //其他用户登陆
            int unOutDevice = RandomUtil.randomInt(3);
            int unLab = RandomUtil.randomInt(3);

            int outDevice = RandomUtil.randomInt(3);
            int labPass = RandomUtil.randomInt(3);

            model.addAttribute("unOutDevice", unOutDevice);
            model.addAttribute("unLab", unLab);

            model.addAttribute("outDevice", outDevice);
            model.addAttribute("labPass", labPass);

            //获取待审批的实验室领用单
            model.addAttribute("labList", new ArrayList<>());

            //获取最近登陆的五个用户
            List<User> userList = userService.lastLoginUser();
            model.addAttribute("userList", userList);
            return jumpPage("console2");
        }
    }

    /*
     * @Description: 进7日设备维修情况
     * @Param:
     * @return:
     */
    @GetMapping("repair")
    public Result taskdata() {
        List<Summation> list = randomSummationList();
        return Result.success(list);
    }

    private List<Summation> randomSummationList() {
        List<Summation> list = new ArrayList<>();
        list.add(new Summation("测试设备", RandomUtil.randomBigDecimal()));
        return list;
    }


}
